import { Component, Injector, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ConfigModule } from 'src/app/config.module';
import { Router } from '@angular/router';
import { Subject, Subscription } from 'rxjs';
import { listAnimation } from '../../../../animations/list.animation';
import { BaseUI } from '../../../../common/base';
import { AssignUserOptionComponent } from '../component/assign-user-option/assign-user-option.component';
import {
    EventKeyEnum,
    EventPublishDTO,
    OptionTypeEnum,
    TableOptionModel
} from '../../../../components/table/table-option/table-option.model';
import { TableComponent } from '../../../../components/table/table.component';
import { ColumnType } from '../../../../components/table/enums/ColumnType';
import { NzTreeNodeOptions } from 'ng-zorro-antd/core/tree/nz-tree-base-node';
import { DateUtil } from '../../../../utils/DateUtil';
import { CancelAuthOptionComponent } from '../component/cancel-auth-option/cancel-auth-option.component';
import { AssignUserService } from '../service/assign-user.service';
import { TableColumnModel } from '../../../../components/table/model/tableColumn.model';


@Component({
    selector: 'app-user',
    templateUrl: './index.component.html',
    styleUrls: ['./index.component.scss'],
    animations: [listAnimation],
})
export class AssignUserComponent extends BaseUI implements OnInit, OnDestroy {
    router$: Subscription;
    onDestroy$: Subject<any> = new Subject<any>();
    constructor(
        public _config: ConfigModule,
        private _router: Router,
        private _injector: Injector,
        public mainService: AssignUserService,
    ) {
        super(_injector);
        const urlParts = this._router.url.split('/');
        this.searchObj.roleId = urlParts[urlParts.length - 1];
    }

    ngOnInit() {
    }

    convertTreeToList(data:any[]): Array<NzTreeNodeOptions> {
        if (!data) return;
        let treeData: NzTreeNodeOptions[] = [];
        data.forEach(node => {
            let treeNode: NzTreeNodeOptions={} as NzTreeNodeOptions;
            treeNode.title = node.label;
            treeNode.expanded = true;
            treeNode.key = node.id;
            treeNode.children = this.convertTreeToList(node.children);
            if (!treeNode.children){
                treeNode.isLeaf = true;
            }
            treeData.push(treeNode);
        })
        return treeData
    }

    ngOnDestroy() {
        this.onDestroy$.complete();
    }


    @ViewChild("mainTable")
    mainTable: TableComponent;

    searchObj:any = {
        pageNum: 1,
        deptId: '',
        deptName:'',
        pageSize: this._config.pageSize,
        name: '',
        phonenumber: '',
        emailNumber: '',
        status: "",
    };

    columnArray:TableColumnModel[] = [
        {type: ColumnType.TEXT, name: '用户名称', key: 'userName'},
        {type: ColumnType.TEXT, name: '用户昵称', key: 'nickName'},
        {type: ColumnType.TEXT, name: '邮箱', key: 'email'},
        {type: ColumnType.TEXT, name: '手机号码', key: 'phonenumber'},
        {type: ColumnType.LABEL, name: '状态', key: 'status'},
        {type: ColumnType.TEXT, name: '创建时间', key: 'createTime'},
    ];
    optionModelList: TableOptionModel[] = [
        {
            optionLabel:"取消授权",
            optionLabelType: OptionTypeEnum.TEXT,
            eventKey:EventKeyEnum.CANCEL_AUTH
        }
    ]

    otherOptionModelList: TableOptionModel[] = [
        {
            optionLabel:"添加用户",
            optionLabelType: OptionTypeEnum.BUTTON,
            eventKey:EventKeyEnum.ADD,
        }
    ]



    searchList() {
        if (this.mainTable){
            this.mainTable.list();
        }
    }

    keyEnter($event: KeyboardEvent) {
        if($event.key === 'Enter'){
            this.searchList();
        }
    }

    resetSearchObject(){
        this.searchObj = {
            pageNum: 1,
            pageSize: this._config.pageSize,
        };
        this.datePick=[];
    }


    /**
     * 日期选择
     */

    datePick: string[]=[];
    onDatePick() {
        this.searchObj.params = {};
        this.searchObj.params['beginTime'] = DateUtil.formatDate(this.datePick[0],"yyyy-MM-dd");
        this.searchObj.params['endTime'] = DateUtil.formatDate(this.datePick[1],"yyyy-MM-dd");
    }



    /**
     * 操作弹框
     */
    @ViewChild('assignUserOptionComponent') assignUserOptionComponent: AssignUserOptionComponent;

    optionEvent($event: EventPublishDTO) {
        let data  = $event.publishData;
        switch ($event.eventKey) {
            case EventKeyEnum.ADD:
                this.assignUserOptionComponent.onModalOpen(this.searchObj.roleId);
                break;
            case EventKeyEnum.CANCEL_AUTH:
                this.cancelAuthOptionComponent.onModalOpen(data.userId,data.roleId,data.userName);
                break;
        }
    }




    @ViewChild('cancelAuthOptionComponent') cancelAuthOptionComponent: CancelAuthOptionComponent;


}
